﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.Xml;
using System.IO;

namespace DataProcessing
{
    public class AppConfig
    {

        static AppConfig _instance;

        [XmlIgnore]
        public static AppConfig Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Load();
                }
                return _instance;
            }

        }
        public const string SETTING_FILE = "appconfig.xml";
        public static AppConfig Load()
        {
            
            XmlSerializer serializer = new XmlSerializer(typeof(AppConfig));
            FileStream fs = new FileStream(SETTING_FILE, FileMode.Open);
            TextReader reader = new StreamReader(fs);
            AppConfig ret = serializer.Deserialize(reader) as AppConfig;
            
            fs.Close();
            ret.DefaultSpecie.Load();
            return ret;
        }
        public void SaveSample()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AppConfig));
            FileStream fs = new FileStream("sample" + SETTING_FILE, FileMode.Create);
            TextWriter writer = new StreamWriter(fs);
            serializer.Serialize(writer, this);
            fs.Close();

        }

        public void Save()
        {
            XmlSerializer serializer = new XmlSerializer(typeof(AppConfig));
            FileStream fs = new FileStream(SETTING_FILE, FileMode.Create);
            TextWriter writer = new StreamWriter(fs);
            serializer.Serialize(writer, this);
            fs.Close();

        }
        public AppConfig()
        {
            this.DefaultSpecieName = "";
            this.SpeciesSettings = new List<SpeciesSetting>();
           
            this.PresetPoints = new List<PresetPoint>();
            
        }
        public static void GenerateSampleFile()
        {
            AppConfig setting = new AppConfig();
            setting.SpeciesSettings.Add(new SpeciesSetting());
            setting.PresetPoints.Add(new PresetPoint());
            setting.SaveSample();
            

        }

        public List<SpeciesSetting> SpeciesSettings
        {
            get;
            set;
        }

        public List<PresetPoint> PresetPoints
        {
            get;set;
        }

       const int MAX_PRESET = 4;

       public string DefaultSpecieName
       {
           get;
           set;
       }
       [XmlIgnore]
       public SpeciesSetting DefaultSpecie
       {
           get
           {
               var tmp = from Sepcie in SpeciesSettings
                         where Sepcie.Name.Equals(DefaultSpecieName,StringComparison.CurrentCultureIgnoreCase)
                         select Sepcie;
               if (tmp == null || tmp.Count() == 0)
                   return null;

               return tmp.First();
           }

       }

       public bool AddPresetPoint(PresetPoint value)
       {

           var tmp = GetPresetPointByName(value.Name);
           if(tmp!=null)
               return false;

           if (this.PresetPoints.Count >= MAX_PRESET)
           {
               PresetPoints.RemoveAt(MAX_PRESET - 1);
               PresetPoints.Insert(0, value);
           }
           else
           {
               PresetPoints.Add(value);
           }

           return true;
       }

       public PresetPoint GetPresetPointByName(String value)
       {
           var tmp = from point in PresetPoints
                     where point.Name.Equals(value, StringComparison.CurrentCultureIgnoreCase)
                     select point;
           if (tmp != null && tmp.Count() > 0)
               return tmp.First();
           else
               return null;
       }
    }
}
